/*
 * ECARX Technology Limited is the owner of the copyright and the trade secret of this software.
 * Without permission, no one has the right to obtain, disclose or use this software in any way.
 */

package com.ecarx.systemui.plugin.ui.navigationbar;

import android.content.Context;
import android.content.res.Configuration;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;

import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.content.res.AppCompatResources;

import com.ecarx.systemui.plugin.AppConstants;
import com.ecarx.systemui.plugin.R;
import com.ecarx.systemui.plugin.controller.hvac.SeatSignalController;
import com.ecarx.systemui.plugin.ui.dialog.PassengerSeatDialog;
import com.ecarx.systemui.plugin.ui.dialog.base.DialogManager;
import com.ecarx.systemui.plugin.model.listener.NoDoubleClickListener;
import com.ecarx.systemui.plugin.model.listener.SimpleSeatDialogCallBack;
import com.ecarx.systemui.plugin.model.listener.SimpleSeatFuncNotifyViewListener;
import com.ecarx.systemui.plugin.utils.LogUtils;
import com.ecarx.xui.adaptapi.FunctionStatus;

public class NavigationBarRightSeatView extends FrameLayout {

    private static final String TAG = "NavigationBarRightSeatViewTag";

    private ImageView ivSeatHeat;
    private ImageView ivSeatVentilation;

    private SeatSignalController seatSignalController;

    public NavigationBarRightSeatView(@NonNull Context context) {
        this(context, null);
    }

    public NavigationBarRightSeatView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public NavigationBarRightSeatView(@NonNull Context context,
                                      @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private final SimpleSeatDialogCallBack seatDialogCallBack = new SimpleSeatDialogCallBack() {
        @Override
        public void adjustPassengerSeatHeat() {
            if (seatSignalController != null) {
                seatSignalController.adjustPassengerSeatHeat();
            }
        }

        @Override
        public void adjustPassengerSeatVent() {
            if (seatSignalController != null) {
                seatSignalController.adjustPassengerSeatVent();
            }
        }
    };

    private void init() {
        LayoutInflater.from(getContext()).inflate(R.layout.navigation_right_seat,
                this, true);
        ivSeatHeat = findViewById(R.id.iv_seat_heat);
        ivSeatVentilation = findViewById(R.id.iv_seat_ventilation);

        ivSeatHeat.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View view) {
                if (seatSignalController == null) {
                    return;
                }
                boolean supportPassengerSeatVent = seatSignalController.isSupportPassengerSeatVent();
                LogUtils.d(TAG, "ivSeatHeat CLICK supportPassengerSeatVent:" + supportPassengerSeatVent);
                if (supportPassengerSeatVent) {
                    int[] location = new int[2];
                    ivSeatHeat.getLocationOnScreen(location);
                    location[0] = location[0] + ivSeatHeat.getWidth() / 2;
                    location[0] = location[0] - PassengerSeatDialog.DIALOG_TOTAL_WIDTH / 2; //最终dialog 显示的x坐标
                    PassengerSeatDialog passengerSeatDialog = DialogManager.getDialogManager()
                            .showPassengerSeatDialog(getContext(), location);
                    passengerSeatDialog.setDialogCallBack(seatDialogCallBack);

                    Integer passengerSeatHeatValue = seatSignalController.getPassengerSeatHeatValue();
                    Integer passengerSeatVentValue = seatSignalController.getPassengerSeatVentValue();
                    passengerSeatDialog.updateSignalValue(passengerSeatHeatValue, passengerSeatVentValue);

                } else {
                    seatSignalController.adjustPassengerSeatHeat();
                }
            }
        });

        ivSeatVentilation.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View view) {
                if (seatSignalController == null) {
                    return;
                }
                boolean supportPassengerSeatHeat = seatSignalController.isSupportPassengerSeatHeat();
                LogUtils.d(TAG, "ivSeatVentilation CLICK supportPassengerSeatHeat:" + supportPassengerSeatHeat);
                if (supportPassengerSeatHeat) {
                    int[] location = new int[2];
                    ivSeatVentilation.getLocationOnScreen(location);
                    location[0] = location[0] + ivSeatVentilation.getWidth() / 2;
                    location[0] = location[0] - PassengerSeatDialog.DIALOG_TOTAL_WIDTH / 2; //最终dialog 显示的x坐标
                    PassengerSeatDialog passengerSeatDialog = DialogManager.getDialogManager()
                            .showPassengerSeatDialog(getContext(), location);
                    passengerSeatDialog.setDialogCallBack(seatDialogCallBack);

                    Integer passengerSeatHeatValue = seatSignalController.getPassengerSeatHeatValue();
                    Integer passengerSeatVentValue = seatSignalController.getPassengerSeatVentValue();
                    passengerSeatDialog.updateSignalValue(passengerSeatHeatValue, passengerSeatVentValue);
                } else {
                    seatSignalController.adjustPassengerSeatVent();
                }
            }
        });
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        if (seatSignalController == null) {
            seatSignalController = SeatSignalController.getInstance();
        }
        seatSignalController.setSeatRightNotifyViewListener(seatFuncNotifyListener);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (seatSignalController != null) {
            seatSignalController.setSeatRightNotifyViewListener(null);
        }
    }

    @Override
    protected void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if (ivSeatHeat != null) {
            //ivSeatHeat.setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.right_seat_heating_off));
        }
    }

    public void onSeatHeatValueChanged(Integer value) {
        LogUtils.d(TAG, "onSeatHeatValueChanged value:" + value);
        if (value == null) {
            return;
        }
        int resId = getHeatIconByHeatValue(value);
        updateIcon(ivSeatHeat, resId);
    }

    public void onSeatHeatStatusChanged(FunctionStatus status) {
        LogUtils.d(TAG, "onSeatHeatStatusChanged FuncStatus:" + status);
        updateIconStatus(ivSeatHeat, status);
    }

    public void onSeatVentilationValueChanged(Integer value) {
        LogUtils.d(TAG, "onSeatVentilationValueChanged value:" + value);
        if (value == null) {
            return;
        }
        int resId = getVentilationIconByVentilationValue(value);
        updateIcon(ivSeatVentilation, resId);
    }

    public void onSeatVentilationStatusChanged(FunctionStatus status) {
        updateIconStatus(ivSeatVentilation, status);
    }

    private int getVentilationIconByVentilationValue(Integer value) {
        switch (value) {
            case AppConstants.HEAT_LEVEL_0:
                return R.drawable.right_seat_ventilation_off;
            case AppConstants.HEAT_LEVEL_1:
                return R.drawable.right_seat_ventilation_1;
            case AppConstants.HEAT_LEVEL_2:
                return R.drawable.right_seat_ventilation_2;
            case AppConstants.HEAT_LEVEL_3:
                return R.drawable.right_seat_ventilation_3;
        }
        LogUtils.d(TAG, "NO SEAT VENTILATION VALUE value:" + value);
        return 0;
    }

    private int getHeatIconByHeatValue(Integer value) {
        switch (value) {
            case AppConstants.HEAT_LEVEL_0:
                return R.drawable.right_seat_heating_off;
            case AppConstants.HEAT_LEVEL_1:
                return R.drawable.right_seat_heating_1;
            case AppConstants.HEAT_LEVEL_2:
                return R.drawable.right_seat_heating_2;
            case AppConstants.HEAT_LEVEL_3:
                return R.drawable.right_seat_heating_3;
        }
        LogUtils.d(TAG, "NO SEAT HEAT VALUE value:" + value);
        return 0;
    }

    private void updateIcon(ImageView imageView, @DrawableRes int resId) {
        post(() -> imageView.setImageDrawable(AppCompatResources.getDrawable(getContext(), resId)));
    }

    private void updateIconStatus(View view, FunctionStatus status) {
        postDelayed(() -> {
            if (FunctionStatus.active == status) {
                if (view != null) {
                    view.setAlpha(1f);
                    view.setEnabled(true);
                }
            } else {
                if (view != null) {
                    view.setAlpha(0.5f);
                    view.setEnabled(false);
                }
            }
            //这里可以在统一判断下2个功能的状态，统一修改一遍
            if (seatSignalController == null) {
                return;
            }

            boolean supportPassengerSeatHeat = seatSignalController.isSupportPassengerSeatHeat();
            boolean supportPassengerSeatVent = seatSignalController.isSupportPassengerSeatVent();


            LogUtils.d(TAG, "IS Active supportPassengerSeatHeat:" + supportPassengerSeatHeat +
                    ", supportPassengerSeatVent:" + supportPassengerSeatVent);

            if (supportPassengerSeatHeat || supportPassengerSeatVent) {
                if (supportPassengerSeatHeat && supportPassengerSeatVent) {
                    int passengerSeatHeatValue = seatSignalController.getPassengerSeatHeatValue();
                    if (255 != passengerSeatHeatValue &&
                            AppConstants.HEAT_LEVEL_0 != passengerSeatHeatValue) {
                        ivSeatHeat.setVisibility(VISIBLE);
                        ivSeatVentilation.setVisibility(GONE);
                    } else {
                        ivSeatVentilation.setVisibility(VISIBLE);
                        ivSeatHeat.setVisibility(GONE);
                    }
                } else {
                    if (supportPassengerSeatHeat) {
                        ivSeatHeat.setVisibility(VISIBLE);
                        ivSeatVentilation.setVisibility(GONE);
                    } else {
                        ivSeatVentilation.setVisibility(VISIBLE);
                        ivSeatHeat.setVisibility(GONE);
                    }
                }
            } else {
                //副驾驶座椅加热、通风都不是active
                ivSeatHeat.setVisibility(GONE);
                ivSeatVentilation.setVisibility(GONE);
            }
            if (ivSeatHeat.getVisibility() == GONE && ivSeatVentilation.getVisibility() == GONE) {
                setVisibility(GONE);
            } else {
                setVisibility(VISIBLE);
            }
        }, 10);
    }

    //------------------------------------信号回调开始------------------------------------------------

    private final SimpleSeatFuncNotifyViewListener seatFuncNotifyListener =
            new SimpleSeatFuncNotifyViewListener() {

                @Override
                public void notifyPassengerSeatHeatChanged(int value, FunctionStatus functionStatus) {
                    super.notifyPassengerSeatHeatChanged(value, functionStatus);
                    onSeatHeatValueChanged(value);
                    onSeatHeatStatusChanged(functionStatus);
                }

                @Override
                public void notifyPassengerSeatVentChanged(int value, FunctionStatus functionStatus) {
                    super.notifyPassengerSeatVentChanged(value, functionStatus);
                    onSeatVentilationValueChanged(value);
                    onSeatVentilationStatusChanged(functionStatus);
                }

            };
    //------------------------------------信号回调结束------------------------------------------------
}
